#from msgpasser import *;
from msg import *;
import sys;
from configuration import yaml_configuration

nameIpPorts = ''
comGraph = {}
init = 0
ipList = {}

#http://www.python.org/doc/essays/graphs.html
def find_shortest_path(graph, start, end, path=[]):

    #print "Start %s end %s" % (start, end)

    path = path + [start]
    if start == end:
        return path
    if not graph.has_key(start):
        return None
    shortest = None
    for node in graph[start]:
        if node not in path:
            newpath = find_shortest_path(graph, node, end, path)
            if newpath:
                if not shortest or len(newpath) < len(shortest):
                    shortest = newpath
    return shortest

def get_graph():
    strRet = ''
    for index in comGraph:
        strRet = strRet + '%' + index
        for node in comGraph[index]:
            strRet = strRet + ';' + node
    return strRet

'''
def new_graph(g):
    global comGraph
    comGraph = g.clone()
    #comGraph = copy.deepycopy(g)
'''

def new_node(name, ip, port, neighbors):
    #find closest node?
    global ipList
    global comGraph
    global nameIpPorts
    #print 'before: '+ nameIpPorts
    nameIpPorts = nameIpPorts + name + ';' +  str(ip) + ';' + str(port) + '%'
    #print nameIpPorts
    ipList[name] = [ip, port]
    comGraph[name] = neighbors
    for n in neighbors:
        list = comGraph[n]
        list.append(name)
        comGraph[n] = list

    return


def nextHop(G, start, end):
    shortestPath = find_shortest_path(G, start, end)
    if len(shortestPath) > 1:
        return shortestPath[1]
    else:
        return "BAD"

def remove_edge(src, dst):
    global init
    global comGraph
    if(src not in comGraph):
        return "Error"
    if(dst not in comGraph):
        return "Error"
    neighbors = comGraph[src]
    if(dst in neighbors):
        neighbors.remove(dst)
        #print 'removing ',
        #print neighbors
        if len(neighbors) == 1:
            #print 'deleting dst %s : ' % (src)
            del comGraph[src]
            #print "del 1 comGraph is : ",
            #print comGraph
        else :
            comGraph[src] = neighbors
    neighbors = comGraph[dst]
    #print "Before ",
    #print neighbors
    if(src in neighbors):
        neighbors.remove(src)
        #print 'removing1 ',
        #print neighbors
        if len(neighbors) == 1:
            #print 'deleting dst %s : ' % (dst)
            del comGraph[dst]
            #print "del 2 comGraph is : ",
            #print comGraph
        else :
            comGraph[dst] = neighbors
    #print "after comGraph is : ",
    #print comGraph
    return "Success"

def getForwardingTable(source):
    global init
    global comGraph
    global ipList
    table = {}
    for data in comGraph:
        if (data != source):
            table[data] = nextHop(comGraph, source, data)
    return table

def initialize_graph(yamlUrl, source):
    global init
    global comGraph
    global ipList
    myConfigFile = yaml_configuration(yamlUrl)
    configuration = myConfigFile.getConfiguration()
    table = {};
    for data in configuration:
        comGraph[data['name']] = data['group']
        ipList[data['name']] = [data['ip'], data['port']]
    for data in configuration:
        if(data['name'] != source):
            table[data['name']] = nextHop(comGraph, source, data['name'])
    #print comGraph
    return table

def get_node_neighbors(node):
    global comGraph
    #print "ComGrpah is : "
    #print comGraph
    if node not in comGraph:
        return "Error"
    else:
        return comGraph[node]
    #myConfigFile = configfile(yamlUrl)
    #configuration = myConfigFile.getConfiguration()
    #graph = {};
    #table = {};
    #for data in configuration:
    #    if(data['name'] == node):
    #        return data['group']
    #return 0
if __name__ == '__main__':
    initialize_graph('demo.yaml', 'Office 108')
